﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using FimetContoDeposito.BLL.Facades;
using FimetContoDeposito.BLL.ResultSets;
using FimetContoDeposito.DAL;
using GrayParrot.Core;
using GrayParrot.Web.Controls;
using GrayParrot.Web.Formly;

namespace FimetContoDeposito.Controllers
{
    public class CronologiaMovimentiController : ApiController
    {
        [HttpGet]
        public string LoadCustomersFields()
        {
            string retValue = null;

            var m_fields = new FormlyFactory()
                   .AddSelect("selMagazzini", "Magazzini", "", "seleziona il magazzino", false, true, null, "/api/Common/LoadCustomers", null, null, null, false)
                   .Create();

            retValue = FormlyHelper.Serialize(m_fields);

            return retValue;
        }

        [HttpPost]
        public System.Net.Http.HttpResponseMessage LoadMovimenti(AnagraficaDTParameters data)
        {
            System.Net.Http.HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            string m_customer = data.Customer;

            DTResult<FCD_CaricaDatiCronologiaReturnModel> result = DTResult<FCD_CaricaDatiCronologiaReturnModel>.Empty("Nessun elemento trovato");

            if (!string.IsNullOrWhiteSpace(m_customer))
            {
                string m_idC = data.Customer.SanitizeDropdownItem();
                Guid m_id = Guid.Parse(m_idC);

                using (MovimentiFacade m_dc = MovimentiFacade.Create())
                {
                    List<FCD_CaricaDatiCronologiaReturnModel> m_objs = m_dc.GetCronologia();

                    CaricaDatiCronologiaResultSetResultSet m_rs = new CaricaDatiCronologiaResultSetResultSet(data, m_objs.AsQueryable());
                    result = m_rs.GetResult();

                    retValue = Request.CreateResponse(HttpStatusCode.OK, result);
                }
            }
            else
            {
                retValue = Request.CreateResponse(HttpStatusCode.OK, result);
            }

            return retValue;
        }
    }
}